package com.rt.easyjava.base.biz1.service.impl;

import com.rt.easyjava.base.biz1.bean.TmptAuthUserBiz;
import com.rt.easyjava.base.biz1.mapper.TmptAuthUserBizMapper;
import com.rt.easyjava.base.biz1.service.intf.TmptAuthUserBizService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-06-13
 */
@Slf4j
@Service
public class TmptAuthUserPwdBizServiceImpl {
    @Resource
    private TmptAuthUserBizMapper tmptAuthUserBizMapper;
    @Resource
    private TmptAuthUserBizService tmptAuthUserBizService;

    public final static int _LoginTimesFailure = 6;
    public final static int _TimeOut_Minute = 30;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result changeUserInfoWhenPsdFailure(String loginname) {
        TmptAuthUserBiz userBiz = tmptAuthUserBizService.selectByKey("loginname",loginname);
        int loginTimesFailure = userBiz.getLogintimesFailure()==null?0:userBiz.getLogintimesFailure().intValue();

        userBiz.setLogintimesFailure(loginTimesFailure+1);
        userBiz.setLogintimeFailureLast(DateTool.getCurrentDatetime());
        tmptAuthUserBizMapper.updateById(userBiz);

        return Result.builder(loginTimesFailure).success();
    }

    // 密码连续错误6次，锁定10分钟
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result validateBeforeLogin(String loginname) {
        TmptAuthUserBiz userBiz = tmptAuthUserBizService.selectByKey("loginname",loginname);
        int loginTimesFailure = userBiz.getLogintimesFailure()==null?0:userBiz.getLogintimesFailure().intValue();
        Date logintimeFailureLast = userBiz.getLogintimeFailureLast();
        if (logintimeFailureLast==null) return Result.builder();

        if (loginTimesFailure>=_LoginTimesFailure) {
            int minute = DateTool.between_minutes(logintimeFailureLast, DateTool.getCurrentDatetime()).intValue();
            System.out.println("------> minute:"+minute);
            if (minute<_TimeOut_Minute) {
                return Result.builder().fault("账户锁定中，"+_TimeOut_Minute+"分钟后自动解锁");
            }
            // 超过10分钟，解锁
            userBiz.setLogintimesFailure(0);
            userBiz.setLogintimeFailureLast(DateTool.getCurrentDatetime());
            tmptAuthUserBizMapper.updateById(userBiz);
        }

        return Result.builder();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public Result changeUserInfoWhenLoginSuc(String loginname) {
        TmptAuthUserBiz userBiz = tmptAuthUserBizService.selectByKey("loginname",loginname);
        int loginTimes = userBiz.getLogintimes()==null?0:userBiz.getLogintimes().intValue();

        userBiz.setLogintimes(loginTimes+1);
        userBiz.setLogintimesFailure(0);
        userBiz.setLogintimelast(DateTool.getCurrentDatetime());
        tmptAuthUserBizMapper.updateById(userBiz);

        return Result.builder().success();
    }

}
